package com.cloud.data.impl;

import com.cloud.config.AbstractBaseDataManager;
import com.cloud.data.UserDataManager;
import com.cloud.dto.UserDto;
import com.cloud.util.JsonUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author wangjian (jian.wang@bertelsmann.com.cn)
 * @Date 2017/1/16 15:18
 */
@Component
public class UserDataManagerImpl extends AbstractBaseDataManager implements UserDataManager {
    private static final Logger logger = LoggerFactory.getLogger(UserDataManagerImpl.class);

    @Autowired
    JsonUtils jsonUtils;

    @Override
    public UserDto getUserData(String key) {

        UserDto result = null;
        Object o = valOps.get(key);

        if (o != null) {
            try {
                result = jsonUtils.parse(o.toString(), UserDto.class);
            } catch (Exception e) {
                logger.error(e.toString());
            }
        }
        return result;
    }

    @Override
    public void setUserData(String key, UserDto user) {
        valOps.set(key, user.toString());
    }

}
